﻿using System;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Utilities;

namespace SP.KeywordsTranslator
{
    public static class Utility
    {
        // Wraps the SPUtility method of the same name.
        public static string GetLocalizedString(string resourceName, int LCID)
        {
            if (string.IsNullOrEmpty(resourceName))
                return string.Empty;

            // SPUtility.GetLocalized string needs a resource expression as the first argument.
            string resourceExpression = string.Format("$Resources:{0}", resourceName);

            string resourceFile = "KeywordsTranslator";

            // Note: If the named resource does not have a value for the specified language,
            // SPUtility.GetLocalizedString returns the value for the invariant language.
            // If the named resource does not exist, it returns the original expression.
            return SPUtility.GetLocalizedString(resourceExpression, resourceFile, (uint)LCID);
        }

        public static void RegisterJSScript(string scriptPath, WebPart webpart)
        {
            string IncludeScriptFormat = @"<script language=""{0}"" src=""{1}""></script>";
            if (!webpart.Page.ClientScript.IsClientScriptBlockRegistered(scriptPath))
            {
                string includescript = String.Format(IncludeScriptFormat, "javascript", scriptPath);
                webpart.Page.ClientScript.RegisterClientScriptBlock(webpart.GetType(), scriptPath, includescript);
            }
        }
    }
}